﻿@using Microsoft.AspNetCore.Mvc.Controllers
@using Smartstore.Engine.Modularity

@inject IModuleCatalog ModuleCatalog
@inject ModuleManager ModuleManager

@{
    Layout = "_AdminLayout";

    var module = ViewBag.Module as IModuleDescriptor;

    if (module == null)
    {
        var controllerType = (ViewContext.ActionDescriptor as ControllerActionDescriptor).ControllerTypeInfo;
        module = ModuleCatalog.GetModuleByAssembly(controllerType.Assembly);
        if (module == null)
        {
            var message = $"Failed to resolve the module descriptor. The '_ConfigureModule' layout requires an instance of '{nameof(IModuleDescriptor)}' which you can pass via 'ViewBag.Module'. ";
            message += $"If you don't pass the descriptor, make sure the originating controller is part of a module assembly.";
            throw new InvalidOperationException(message);
        }
    }

    var systemName = module.SystemName;
    var friendlyName = ModuleManager.GetLocalizedFriendlyName(module);
    var iconUrl = Url.Content(ModuleManager.GetIconUrl(module));
    var returnUrl = Context.GetRouteValueAs("returnUrl", Url.Action("List", "Module"));

    ViewBag.Title = T("Admin.Common.Configure").Value + " - " + friendlyName;
}

<div class="section-header">
    <div class="title" title="@ViewBag.Title (@systemName)">
        <a sm-backto href='@returnUrl'></a>
        <img src="@iconUrl" alt="@friendlyName" />
        <span>@ViewBag.Title</span>
    </div>
    <div class="options">
        <zone name="admin_button_toolbar_before" />
        <a class="update-string-resources btn btn-secondary" href='@Url.Action("UpdateStringResources", "Module")' data-system-name="@systemName">
            <i class="fa fa-rotate"></i>
            <span>@T("Admin.Configuration.Plugins.Resources.Update")</span>
        </a>
        <zone name="admin_button_toolbar_after" />
    </div>
</div>

<div class="plugin-config-container" attr-class='(ViewBag.IsGrid == true, "spa-stretch")'>
    @RenderBody()
</div>

<script sm-target-zone="scripts" data-origin="ConfigureModule">
    $(function () {
        $('.section-header .options').on('click', '[type=submit]', function (e) {
            // On SubmitButtonClick, post the form programmatically, as the button is not a child of the form...
            var btn = $(e.currentTarget);
            let form = !!(btn.data("target"))
                ? $(btn.data("target")).first()
                : $('.plugin-config-container form').first();

            if (form.length) {
                // ...but first add a hidden input to the form with button's name and value to mimic button click WITHIN the form.
                form.prepend($('<input type="hidden" name="' + btn.attr('name') + '" value="' + btn.attr('value') + '" />'));
                form.submit();
            }
        });

        $(".update-string-resources").on("click", function (e) {
            e.preventDefault();

            var $el = $(this);

            $({}).ajax({
                type: 'POST',
                url: $el.attr("href"),
                data: {
                    "systemName": $el.attr("data-system-name")
                },
                success: function (data) {
                    displayNotification(data.Message, data.Success ? "success" : "error");
                }
            });

            return false;
        })
    });
</script>
